
//jQuery.noConflict();

;(function($){

var currentGroup = null;
var currentFile = null;  //not used now
var baseLoc = "http://picfoo.wordjp.com/"
var baseURL = baseLoc + 'php_service/noriGetGroup.php?direction=';
var objSlider;
var isIPhone;
var isIPad;

jQuery(document).ready(entryMain);

function entryMain()
{
  var galleryOption = null;
  //
  var deviceAgent = navigator.userAgent.toLowerCase();
  
  isIPhone = deviceAgent.match(/(iphone|ipod)/);
  isIPad = deviceAgent.match(/(ipad)/);
  
 
  galleryOption= {menu : "simple", fade : true, autohide : false, clickable : false, swipable : true, caption : true, clickhide: true, fullscreen: true};    

  //jQuery(".jbgallery").css({width: 320, height:460});
 

  objSlider = jQuery(".jbgallery").jbgallery(galleryOption, true);
  objSlider.setNextGroup(nextGroup);
  objSlider.setPrevGroup(prevGroup);
  objSlider.setPicChange(picChanged);
  getCurrentInfo();
  if(currentGroup == null)
  {
    nextGroup();
  }
  else
  {
    historyGroup();          
  }
  
}

function picChanged(index)
{
  currentFile = index;
  saveCurrentInfo(); 
}

function nextGroup()
{
  var requestURL = baseURL + "next";        
  if(currentGroup != null)
  {
    requestURL = requestURL + '&currentgroup=' + currentGroup;
  }
  
  $.getJSON(requestURL,
    function(data) {
      populateDataToGallery(data);
      objSlider.go(1);
  });               
}

function prevGroup(showLast)
{
  var requestURL = baseURL + "previous";        
  if(currentGroup != null)
  {
    requestURL = requestURL + '&currentgroup=' + currentGroup;
  }
  
  $.getJSON(requestURL,
    function(data) {
      populateDataToGallery(data);
      if(showLast){
        objSlider.go(data.groupFiles.length);
      }
      else{
        objSlider.go(1);
      }
  });               
}

function historyGroup()
{
  var requestURL = baseURL + "current";        
  if(currentGroup != null)
  {
    requestURL = requestURL + '&currentgroup=' + currentGroup;
  }
  
  $.getJSON(requestURL,
    function(data) {
      populateDataToGallery(data);     
      objSlider.go(currentFile);
  }); 
}

function populateDataToGallery(data)
{
  var index;
  
  objSlider.empty();
  for(index = 0; index < data.groupFiles.length; index++)
  {          
    var imageObj = {
      href : baseLoc + "pic/" + data.groupName + "/" + data.groupFiles[index],
      title : data.groupTitle,
      caption: data.fileDescriptions[index]
    };          
    objSlider.push(imageObj);          
  }                          
  
  currentGroup = data.groupName;
  saveCurrentInfo();
}

function getCurrentInfo()
{
  currentGroup = $.cookie('current_group');
  currentFile = parseInt($.cookie('current_file'));        
}

function saveCurrentInfo()
{
  $.cookie('current_group', currentGroup,{ expires: 14 });
  $.cookie('current_file', currentFile.toString(),{ expires: 14 });
}


})(jQuery);
